<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\api\model\learn\article;

use app\api\service\User as UserService;
use app\common\model\learn\article\Title as BookModel;
use cores\exception\BaseException;
use think\facade\Db;

/**
 * 商品评价模型
 * Class Article
 * @package app\api\model
 */
class Book extends BookModel {
	/**
	 * 隐藏字段
	 * @var array
	 */
	protected $hidden = [

		'is_delete',
		'store_id',
		'create_time',
		'update_time',
	];

	public static function list2(int $book_id) {
		$userId = UserService::getCurrentLoginUserId();

		// 获取文章详情
		$detail = Db::name('learn_article_book')->where('category_id', $book_id)->find();
		if (empty($detail)) {
			throwError('很抱歉，当前条文不存在');
		} else {
			$date = Date('Y-m-d');
			$list = Db::name('learn_article_user')->alias('u')
				->join('learn_article a', 'a.article_id=u.article_id')
				->where('u.user_id', $userId)
				->where('u.book_id', $book_id)
				->where('u.date', $date)
				->field('u.*,a.book_name,a.book_name2,a.content')
				->select()->toArray();
			if ($list) {
			} else {
				$all_list = Db::name('learn_article_user')->where('user_id', $userId)
					->where('book_id', $book_id)->field('article_id')->select()->toArray();
				$ids = array_column($all_list, 'detail_id');

				$limit = Db::name('learn_article_book_user')->where('user_id', $userId)
					->where('book_id', $book_id)->value('num');
				$arr = Db::name('learn_article')
					->where('book_name', $detail['name'])
					->where('article_id', 'not in', $ids)
					->field('article_id')
					->limit($limit)->select()->toArray();

				foreach ($arr as $key => $value) {
					$arr[$key]['store_id'] = self::$storeId;
					$arr[$key]['create_time'] = time();
					$arr[$key]['user_id'] = $userId;
					$arr[$key]['date'] = $date;
					$arr[$key]['book_id'] = $book_id;
				}

				Db::name('learn_article_user')->insertAll($arr);

				$list = Db::name('learn_article_user')->alias('u')
					->join('learn_article a', 'a.article_id=u.article_id')
					->where('u.user_id', $userId)
					->where('u.book_id', $book_id)
					->where('u.date', $date)
					->field('u.*,a.book_name,a.book_name2,a.content')
					->select()->toArray();

			}

		}

		return $list;
	}

	/**
	 * 获取文章详情
	 * @param int $articleId 文章ID
	 * @return Article|array|null
	 * @throws \cores\exception\BaseException
	 */
	public static function getDetail(int $articleId) {
		// 获取文章详情
		$detail = parent::detail($articleId);
		if (empty($detail) || $detail['is_delete']) {
			throwError('很抱歉，当前条文不存在');
		}

		return $detail;
	}

	/**
	 * 累积文章实际阅读数
	 * @param int $articleId 文章ID
	 * @return void
	 */
	private static function setIncActualViews(int $articleId): void {
		(new static )->setInc($articleId, 'actual_views', 1);
	}

	/**
	 * 获取文章列表
	 * @param int $categoryId
	 * @param int $limit
	 * @return \think\Paginator
	 * @throws \think\db\exception\DbException
	 */
	public function getList($book_name ='', int $limit = 15): \think\Paginator {
		// 检索查询条件
		$filter = [];
		$book_name!='' && $filter[] = ['book_name', '=', $book_name];
		// 获取列表数据
		return $this
		
			->where($filter)
			->where('status', '=', 1)
			->where('is_delete', '=', 0)
			->order(['create_time' => 'asc'])
			->paginate($limit);
	}
}